package cn.zwx.activiti.example.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.zwx.activiti.example.common.enums.FormDataParamType;
import cn.zwx.activiti.example.model.bo.FormDataBO;
import cn.zwx.activiti.example.model.req.FormDataShowReq;
import cn.zwx.activiti.example.model.resp.FormDataShowResp;
import com.alibaba.fastjson.JSON;
import org.activiti.api.task.model.Task;
import org.activiti.bpmn.model.FormProperty;

import javax.xml.crypto.Data;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @projectName:activiti-example
 * @see:cn.zwx.activiti.example.utils
 * @author:zhangwenxue
 * @createTime:2021/6/3 22:52
 * @version:1.0
 */
public class FormDataUtil {

    private  static final String  step = "-_!";
    /**
     * @description 多个控件间隔
     **/
    private  static final String mul_step = "!_!";

//    FormProperty_0ueitp2-_!类型-_!名称-_!默认值-_!是否参数
//    例子：
//    FormProperty_0lovri0-_!string-_!姓名-_!请输入姓名-_!f
//    FormProperty_1iu6onu-_!int-_!年龄-_!请输入年龄-_!s
//
//    默认值：无、字符常量、FormProperty_开头定义过的控件ID
//    是否参数：f为不是参数，s是字符，t是时间(不需要int，因为这里int等价于string)
//    注：类型是可以获取到的，但是为了统一配置原则，都配置到
    /**
     * @description 格式化表单数据
     * @param formId 表单id
     * @param type 类型
     * @param name 名称
     * @param defaultValue 默认值
     * @param hasParam 是否参数
     * @return java.lang.String
     * @author zhangwenxue
     * @createTime 2021/6/3 22:58
     **/
    public static String formatFormData(String formId,String type,String name,String defaultValue,String hasParam){
        return formId + step + type + step + name +step+ defaultValue + step +hasParam;
    }

    public static void main(String[] args) {
        String str = "[\n" +
                "    {\n" +
                "      \"controlId\": \"FormProperty_1rfij3v\",\n" +
                "      \"controlType\": \"boolean\",\n" +
                "      \"controlDesc\": \"audit\",\n" +
                "      \"controlDefValue\": \"true\",\n" +
                "      \"controlIsParam\": \"y\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"controlId\": \"FormProperty_0de0l2h\",\n" +
                "      \"controlType\": \"string\",\n" +
                "      \"controlDesc\": \"reason\",\n" +
                "      \"controlDefValue\": \"同意啦啊\",\n" +
                "      \"controlIsParam\": \"n\"\n" +
                "    }\n" +
                "  ]";
        List<FormDataShowResp> reqs = JSON.parseArray(str, FormDataShowResp.class);
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < reqs.size(); i++) {
            FormDataShowResp resp = reqs.get(i);
            String format = formatFormData(resp.getControlId(),
                    resp.getControlType(), resp.getControlDesc(),
                    resp.getControlDefValue(), resp.getControlIsParam());
            if (i== reqs.size()-1){
                buffer.append(format);
            }else {
                buffer.append(format).append(mul_step);
            }
        }
        System.out.println(buffer.toString());
          
    }
    /**
     * @description 解析表单数据
     * @return java.lang.String
     * @author zhangwenxue
     * @createTime 2021/6/3 22:58
     **/
    public  static  List<FormDataShowResp> parasFormData(List<FormProperty> formProperties, Map<String, String> historyMap){
        List<FormDataShowResp> list = CollectionUtil.newArrayList();
        for (FormProperty fp : formProperties) {
            FormDataShowResp formDataShowResp = new FormDataShowResp();
            String[] splitFP = fp.getId().split("-_!");
            formDataShowResp.setControlId(splitFP[0]);
            formDataShowResp.setControlType(splitFP[1]);
            formDataShowResp.setControlDesc(splitFP[2]);
            //默认值如果是表单控件ID
            if (splitFP[3].startsWith("FormProperty_")) {
                //控件ID存在
                if (historyMap.containsKey(splitFP[3])) {
                    formDataShowResp.setControlDefValue(historyMap.get(splitFP[3]));
                } else {
                    //控件ID不存在
                    formDataShowResp.setControlDefValue("读取失败，检查" + splitFP[0] + "配置");
                }
            } else {
                //默认值如果不是表单控件ID则写入默认值
                if (formDataShowResp.getControlType().equals(FormDataParamType.DATE_PARAM.getMsg())){
                    if ("now()".equals(splitFP[3])){
                        formDataShowResp.setControlDefValue(DateUtil.now());
                    }else {
                        formDataShowResp.setControlDefValue(splitFP[3]);
                    }
                }else {
                    formDataShowResp.setControlDefValue(splitFP[3]);
                }

            }
            formDataShowResp.setControlIsParam(splitFP[4]);
            list.add(formDataShowResp);
        }
        return list;
    }

    /**
     * @description 解析表单数据
     * @return java.lang.String
     * @author zhangwenxue
     * @createTime 2021/6/3 22:58
     **/
    public  static  List<FormDataShowResp> parasFormData(List<FormProperty> formProperties){
        List<FormDataShowResp> list = CollectionUtil.newArrayList();
        for (FormProperty fp : formProperties) {
            FormDataShowResp formDataShowResp = new FormDataShowResp();
            String[] splitFP = fp.getId().split("-_!");
            formDataShowResp.setControlId(splitFP[0]);
            formDataShowResp.setControlType(splitFP[1]);
            formDataShowResp.setControlDesc(splitFP[2]);
            //默认值如果不是表单控件ID则写入默认值
            formDataShowResp.setControlDefValue(splitFP[3]);
            formDataShowResp.setControlIsParam(splitFP[4]);
            list.add(formDataShowResp);
        }
        return list;
    }


    /**
     * @description 解析表单数据
     *    //formData:控件id-_!类型-_!名称-_!控件值-_!是否参数!_!控件id-_!类型-_!名称-_!控件值-_!是否参数
     * @return java.lang.String
     * @author zhangwenxue
     * @createTime 2021/6/3 22:58
     **/
    public static  List<FormDataBO> parasFormDataForBO(String formData, Task task){
        List<FormDataBO> list = CollectionUtil.newArrayList();
        String[] formDataList = formData.split("!_!");
        for (String controlItem : formDataList) {
            String[] formDataItem = controlItem.split("-_!");
            FormDataBO po = new FormDataBO();
            po.setControlId(formDataItem[0]);
            po.setControlVaule(formDataItem[3]);
            po.setFormKey(task.getFormKey());
            po.setProcDefId(task.getProcessDefinitionId());
            po.setProcInstId(task.getProcessInstanceId());
            po.setControlType(formDataItem[1]);
            po.setVariableName(formDataItem[2]);
            po.setHasParam(formDataItem[4]);
            list.add(po);
        }
        return list;
    }

}
